<?php

/**
 * This is the model class for table "changesets".
 *
 * The followings are the available columns in table 'changesets':
 * @property integer $id
 * @property integer $repository_id
 * @property string $revision
 * @property string $committer
 * @property string $committed_on
 * @property string $comments
 * @property string $commit_date
 * @property string $scmid
 * @property integer $user_id
 *
 * The followings are the available model relations:
 * @property Changes[] $changes
 * @property Repository $repository
 * @property User $user
 * @property Issues[] $issues
 */
class Changeset extends CActiveRecord
{
    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Changeset the static model class
     */
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'changesets';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('repository_id, revision, committed_on, commit_date', 'required'),
            array('repository_id, user_id', 'numerical', 'integerOnly'=>true),
            array('revision, committer, scmid', 'length', 'max'=>255),
            array('comments', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, repository_id, revision, committer, committed_on, comments, commit_date, scmid, user_id', 'safe', 'on'=>'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'changes' => array(self::HAS_MANY, 'Change', 'changeset_id'),
            'repository' => array(self::BELONGS_TO, 'Repository', 'repository_id'),
            'user' => array(self::BELONGS_TO, 'User', 'user_id'),
            'issues' => array(self::MANY_MANY, 'Issue', 'changesets_issues(changeset_id, issue_id)'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'id' => 'ID',
            'repository_id' => 'Repository',
            'revision' => 'Revision',
            'committer' => 'Committer',
            'committed_on' => 'Committed On',
            'comments' => 'Comments',
            'commit_date' => 'Commit Date',
            'scmid' => 'Scmid',
            'user_id' => 'User',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria=new CDbCriteria;

        $criteria->compare('id',$this->id);
        $criteria->compare('repository_id',$this->repository_id);
        $criteria->compare('revision',$this->revision,true);
        $criteria->compare('committer',$this->committer,true);
        $criteria->compare('committed_on',$this->committed_on,true);
        $criteria->compare('comments',$this->comments,true);
        $criteria->compare('commit_date',$this->commit_date,true);
        $criteria->compare('scmid',$this->scmid,true);
        $criteria->compare('user_id',$this->user_id);

        return new CActiveDataProvider($this, array(
            'criteria'=>$criteria,
        ));
    }
}